//
//  SHComposeTextView.swift
//  SHWeibo
//
//  Created by jingming wang on 16/8/3.
//  Copyright © 2016年 cpy.name. All rights reserved.
//

import UIKit

class SHComposeTextView: UITextView {

    // 站位文字
    private lazy var placeholderLabel: UILabel = UILabel(text: "微博上海IOS三期 微博上海IOS三期 微博上海IOS三期 微博上海IOS三期 微博上海IOS三期 ", textColor: UIColor.lightGrayColor()
        , font: 15, numberOfLines: 0, textAlignment: .Center)
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置界面
    private func setupUI() {
        backgroundColor = UIColor.whiteColor()
        font = Font(15)
        
        alwaysBounceVertical = true
        addSubview(placeholderLabel)
        
        placeholderLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(5)
            make.top.equalTo(self).offset(8)
            make.width.equalTo(self).offset(-10)
        }
        // 通过监听系统通知来得到当前textView 的文字改变
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "valueChange", name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    //MARK: - 通知方法
    func valueChange(){
        placeholderLabel.hidden = self.hasText()
    }
    //MARK: - 移除通知
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self, name: UITextViewTextDidChangeNotification, object: nil)
    }

}
